package com.lovo.test.frame;




import com.lovo.test.Util.AtmMysq;
import com.lovo.test.pojo.UserDO;
import com.lovo.test.service.UserService;
import com.lovo.test.service.impl.UserServiceImpl;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.EOFException;

//转账界面
public class TurnPanel extends JPanel implements ActionListener {
    private UserService userService = new UserServiceImpl();
    private ATM frame;

    private JButton confirm;
    private JButton returnBut;

    private JTextField turnObj;//转账对象文本框
    private JTextField turnMoney;//转账金额文本框
    private JLabel objLabel;
    private JLabel moneyLabel;


    public TurnPanel(ATM frame){
        this.frame=frame;
        //设置空布局
        this.setLayout(null);
        //设置透明
        this.setOpaque(false);

        //确认按钮，确认转账
         confirm = new JButton("确认");
         confirm.setBounds(200,330,140,50);
         this.add(confirm);
         confirm.addActionListener(this);

         //返回按钮，返回菜单界面
        returnBut = new JButton("返回");
        returnBut.setBounds(460,330,140,50);
        this.add(returnBut);
        returnBut.addActionListener(this);

        //以下是文本框
        //转账对象文本框
        turnObj = new JTextField();//账号文本框
        turnObj.setBounds(350,170,100,20);
        this.add(turnObj);

        //转账金额文本框
        turnMoney = new JTextField();//账号文本框
        turnMoney.setBounds(350,200,100,20);
        this.add(turnMoney);

        //以下是标签
        objLabel = new JLabel("请输入收款方");
        objLabel.setBounds(250,170,100,20);
        add(objLabel);

        //以下是标签
        moneyLabel = new JLabel("请输入转账金额");
        moneyLabel.setBounds(250,200,100,20);
        add(moneyLabel);

    }

    //事件监听器
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource()==confirm){//确认转账事件
            String turnObj1=turnObj.getText();//获取转账对象
            int turnMoney1=Integer.parseInt(turnMoney.getText());//获取转账金额,并转成int类型

            try {
                userService.turnMoney(frame.getUserName(),turnObj1,turnMoney1);
            } catch (NullPointerException exception) {
                JOptionPane.showMessageDialog(this,"不能给自己转账或未找到收款方");
            }catch (EOFException e1){
                JOptionPane.showMessageDialog(this,"账户余额不足");
            }catch (Exception e2){

            }
            JOptionPane.showMessageDialog(this,"转账成功");


        }else if (e.getSource()==returnBut){//返回菜单事件
            frame.switchPanel("菜单");

        }

    }
    //转账函数
    public void turnMoneys(){
        String turnObj1=turnObj.getText();//获取转账对象
        int turnMoney1=Integer.parseInt(turnMoney.getText());//获取转账金额,并转成int类型
        frame.setUserName(AtmMysq.getById(frame.getUserName().getId()));//当前登录用户
        UserDO objUser=AtmMysq.getByName(turnObj1);//转账对象用户
        if (objUser==null){
            JOptionPane.showMessageDialog(this,"未找到收款方");
            return;
        }
        if (frame.getUserName().getId()==objUser.getId()){
            JOptionPane.showMessageDialog(this,"不能给自己转账");
            return;
        }
        if (turnMoney1> frame.getUserName().getMoney()){
            JOptionPane.showMessageDialog(this,"账户余额不足");
            return;
        }

        frame.getUserName().setMoney(frame.getUserName().getMoney()-turnMoney1);
        objUser.setMoney(objUser.getMoney()+turnMoney1);
        AtmMysq.turnModify(frame.getUserName(),objUser);
        JOptionPane.showMessageDialog(this,"转账成功");

    }


}
